﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using ConsolidationTool.Common.Entities;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;

namespace ConsolidationTool.WebParts.Flash_Input_QS.QSEditForm
{
    public partial class QSEditFormUserControl : FSUserControlBase
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            TopSelector = (commonEntitySelector as CommonEntitySelector);
            TopSelector.EntitySelectorChanged += new CommonEntitySelector.EntitySelectorChangeHandler(TopSelector_EntitySelectorChanged);
            (commonButtomAction as CommonButtomAction).SaveButtonClicked += new CommonButtomAction.SaveButtonClickHandler(ESNewFormUserControl_SaveButtonClicked);

            ControlMode = Title.ControlMode;
            //if (!IsPostBack)
            //{
            //    BindMR(Year.ItemFieldValue.ToString(), Month.ItemFieldValue.ToString(), CoCd.ItemFieldValue.ToString());
            //}
        }


        void ESNewFormUserControl_SaveButtonClicked(object sender, EventArgs e)
        {
            this.Page.Validate();
            base.Save();
        }

        public override SPListItem Item
        {
            get
            {
                return ConsolidationTool_CoName.ListItem;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!IsPostBack)
                base.FormInitialization();
            base.OnPreRender(e);
        }


        void TopSelector_EntitySelectorChanged(object sender, int EntityID)
        {
            ConsolidationTool_WorkingHours.Value = GetWorkingHours(TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text, TopSelector.lbCoCd.Text);
            BindMR(TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text, TopSelector.lbCoCd.Text);
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Page.Validate();
            if (Page.IsValid)
            {
                SPListItem item = ConsolidationTool_CoCd.ListItem;
           
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                Utilities.CloseForm();
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Utilities.CloseForm();
        }

        /// <summary>
        /// 根据年份
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="coCode"></param>
        /// <returns></returns>
        public int GetWorkingHours(string year, string month, string coCode)
        {
            int hours = 0;
            SPQuery spq = new SPQuery();
            spq.Query = @"<Where>
                           <And>
                            <And>
                             <Eq>
                               <FieldRef Name='Year' />
                               <Value Type='Integer'>" + year + @"</Value>
                             </Eq>
                             <Eq>
                               <FieldRef Name='Month' />
                               <Value Type='Integer'>" + month + @"</Value>
                             </Eq>
                            </And>
                            <Eq>
                               <FieldRef Name='CoCd' />
                               <Value Type='Text' >" + coCode + @"</Value>
                            </Eq>
                           </And>
                        </Where>";
            spq.ViewFields = "<FieldRef Name='HR01030101'/><FieldRef Name='HR01040101'/>";
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["HR"];
                    SPListItemCollection items = list.GetItems(spq);
                    if (items.Count > 0)
                    {
                        string hr01030101 = Common.Utilities.GetItemValue(items[0], "HR01030101");
                        if (string.IsNullOrEmpty(hr01030101))
                            hr01030101 = "0";
                        string hr01040101 = Common.Utilities.GetItemValue(items[0], "HR01040101");
                        if (string.IsNullOrEmpty(hr01040101))
                            hr01040101 = "0";

                        hours = Convert.ToInt32(hr01030101) + Convert.ToInt32(hr01040101);
                    }
                }
            }
            return hours;
        }

        void BindMR(string year, string month, string coCd)
        {
            int QS01010101_Last11Month = 0;
            int QS01010102_Last11Month = 0;
            int QS01010302_Last11Month = 0;
            int QS01010301_Last12Month = 0;

            int m = Convert.ToInt32(month);
            int n = m + 1;
            int y = Convert.ToInt32(year);
            int z = y - 1;

            for (int i = 1; i < m; i++)
            {
                int hr100 = 0;
                int hr101 = 0;
                int hr102 = 0;
                int hr103 = 0;

                string str_hr100 = "";
                string str_hr101 = "";
                string str_hr102 = "";
                string str_hr103 = "";
                string strMonth = i.ToString();

                SPListItem it = Common.BusinessService.GetItem(coCd, year, strMonth, "Quality&Safety");
                if (it != null)
                {
                    str_hr100 = Common.Utilities.GetItemValue(it, "QS01010101");
                    str_hr101 = Common.Utilities.GetItemValue(it, "QS01010102");
                    str_hr102 = Common.Utilities.GetItemValue(it, "QS01010302");
                    str_hr103 = Common.Utilities.GetItemValue(it, "QS01010301");

                    hr100 = (str_hr100 == "" ? 0 : Convert.ToInt32(str_hr100));
                    hr101 = (str_hr101 == "" ? 0 : Convert.ToInt32(str_hr101));
                    hr102 = (str_hr102 == "" ? 0 : Convert.ToInt32(str_hr102));
                    hr103 = (str_hr103 == "" ? 0 : Convert.ToInt32(str_hr103));
                }
                QS01010101_Last11Month += hr100;
                QS01010102_Last11Month += hr101;
                QS01010302_Last11Month += hr102;
                QS01010301_Last12Month += hr103;
            }

            for (int i = n; i <= 12; i++)
            {
                int hr100 = 0;
                int hr101 = 0;
                int hr102 = 0;
                int hr103 = 0;

                string str_hr100 = "";
                string str_hr101 = "";
                string str_hr102 = "";
                string str_hr103 = "";
                string strMonth = i.ToString();
                string strYear = z.ToString();

                SPListItem it = Common.BusinessService.GetItem(coCd, strYear, strMonth, "Quality&Safety");
                if (it != null)
                {
                    str_hr100 = Common.Utilities.GetItemValue(it, "QS01010101");
                    str_hr101 = Common.Utilities.GetItemValue(it, "QS01010102");
                    str_hr102 = Common.Utilities.GetItemValue(it, "QS01010302");
                    str_hr103 = Common.Utilities.GetItemValue(it, "QS01010301");

                    hr100 = (str_hr100 == "" ? 0 : Convert.ToInt32(str_hr100));
                    hr101 = (str_hr101 == "" ? 0 : Convert.ToInt32(str_hr101));
                    hr102 = (str_hr102 == "" ? 0 : Convert.ToInt32(str_hr102));
                    hr103 = (str_hr103 == "" ? 0 : Convert.ToInt32(str_hr103));
                }
                QS01010101_Last11Month += hr100;
                QS01010102_Last11Month += hr101;
                QS01010302_Last11Month += hr102;
                QS01010301_Last12Month += hr103;
            }

            QS01010301_Last12Month = QS01010301_Last12Month + Convert.ToInt32(ConsolidationTool_WorkingHours.Value);

            HiddenField_QS01010101_Last11Month.Value = QS01010101_Last11Month.ToString();
            HiddenField_QS01010102_Last11Month.Value = QS01010102_Last11Month.ToString();
            HiddenField_QS01010302_Last11Month.Value = QS01010302_Last11Month.ToString();
            HiddenField_QS01010301_Last12Month.Value = QS01010301_Last12Month.ToString();
        }
    }
}
